<?php
/**
 * 
 * Form_CreateTag
 * @package Form
 * @filesource /application/forms/CreateTag.php
 * @author Windy
 *
 */
class Form_CreateTag extends Zend_Form
{
	private $formDecorators;
	private $elementDecorators;
	private $buttonDecorators;
	private $hiddenDecorators;
	
	/**
	 * 
	 * Constructor set id and class for form
	 * @param string $id id của form
	 * @param string $class class của form
	 * @param array $arrLanguages Các ngôn được active
	 */
	
    
    public function __construct($id= "", $class = "", $arrLanguages = null)
    {
    	
    	$this->formDecorators = array(
    								array('FormElements'),
    								array('Form',array("name"=>$id, "class"=>$class))
    							);
    						
    	$this->elementDecorators = array(
    									array('Label', array()), 
    									array("HTMLTag",array("tag"=>"div","class"=>"row")),
    									array("Description",array("tag"=>"span"))
    								);
    	$this->buttonDecorators = array(
										array('ViewHelper'),
										array("HTMLTag",array("tag"=>"div","class"=>"buttons"))
										
									);
									
		$this->hiddenDecorators = array(
    									
    									array("HTMLTag",array("tag"=>"div")),
    									array("Description",array("tag"=>"span"))
    								);					
    	//add decorator for form
        $this->addDecorators($this->formDecorators);
        $this->setName('frmAETag');
        
        
        $stringLength = new Zend_Validate_StringLength(4);
        $stringLength->setMessage(
        	'Tên tag phải có ít nhất %min% kí tự',
    		Zend_Validate_StringLength::TOO_SHORT
    	);
        //add name per languages
    	if($arrLanguages != null){
    		foreach ($arrLanguages as $language){
    			//add textbox name= title
        		$txtName = $this->createElement("text", $language->code."_name")
        				->setLabel("Tên thẻ (".$language->name."):")
        				->setAttrib("size", 30)
        				->setRequired()
        				->addValidator($stringLength)
        				->addDecorators($this->elementDecorators);
        		$this->addElement($txtName);
        		
    		}
    	}else{
    		$txtName = $this->createElement("text", "name")
        				->setLabel("Tên tag:")
        				->setRequired(TRUE)
        				->setAttrib("size", 30)
        				->setValidators(array(new Zend_Validate_StringLength(array(8,15))))
        				->setFilters(array(new Zend_Filter_StringToLower()))
        				->addDecorators($this->elementDecorators);
        	$this->addElement($txtName);
    	}
    	//add slug
    	$txtSlug = $this->createElement('text', 'slug')
    					->setLabel("Slug (tên hiển thị trên URL):")
    					->setRequired(TRUE)
    					->setAttrib("size", 30)
    					->setValidators(array(new Zend_Validate_Regex('/[a-z0-9-]*/')))
    					->addDecorators($this->elementDecorators);
    	$this->addElement($txtSlug);
    	
    	
		
		//add textarea name='description'
		$taDescription = $this->createElement('textarea', 'description')
							  ->setLabel('Miêu tả')
							  ->addDecorators($this->elementDecorators);
							 
		$this->addElement($taDescription);
    	//add submit
    	$btnSubmit = $this->createElement('submit', 'submit')
    					->setLabel('Lưu')
    					->addDecorators($this->buttonDecorators);
        $this->addElement($btnSubmit);
        //add reset
        $btnReset = $this->createElement('reset', 'reset')
    					->setLabel('Làm lại')
    					->addDecorators($this->buttonDecorators);
        $this->addElement($btnReset);
    }
    /**
     * (non-PHPdoc)
     * @see Zend_Form::populate()
     */
    public function populate($values, $arrActivedLanguages){
    	
    	parent::populate($values);
    	//điền giá trị vào các trường name
    	foreach($arrActivedLanguages as $language){
    		$name = $this->getElement($language->code.'_name');
    		$name->setValue(Helper_HtmlRender::getTextByLanguage($values['name'], $language->code));
    	}
    	$hddID = $this->createElement('hidden', 'id')
    					->setValue($values['id'])
    					->addDecorators($this->hiddenDecorators);
    	$this->addElement($hddID);
    	
    	
    }
}
?>